package cn.sofwin.boot.crud.web;
import cn.sofwin.boot.crud.dto.LoginParams;
import cn.sofwin.boot.crud.dto.LoginResult;
import cn.sofwin.boot.crud.service.BaUserService;
import cn.sofwin.core.support.api.GenericStatusCode;
import cn.sofwin.core.support.api.RequestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    BaUserService baUserService;

    @PostMapping("login")
    public RequestResult login(@RequestBody LoginParams loginParams){

        System.out.println("login:"+loginParams.toString());
        RequestResult result = new RequestResult();
        LoginResult loginResult = baUserService.login(loginParams); //自上而下的开发
        if(loginResult.getStatusCode() == GenericStatusCode.Success){
            result.appendData("token",loginResult.getToken());
        }else{
            result = new RequestResult(loginResult.getStatusCode());
        }
        return result;
    }
}
